package cn.fruitmarket.shopcar;

import cn.fruitmarket.goods.Fruit;
import cn.fruitmarket.sellstrategy.IStrategy;
import cn.fruitmarket.utils.MoneyUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class CarItem {
    private Fruit fruit;
    private double kilos;
    private double amount;
    List<IStrategy> itemStrategys;
    public CarItem(Fruit fruit,double kilos) throws Exception{
        this.fruit = fruit;
        this.kilos = kilos;
        if(kilos<=0) {
            throw new Exception("称重必须大于0");
        }
        this.itemStrategys = new ArrayList<IStrategy>();
    }
    public void computeAmount(Date createDate){
        double fruitMoney = MoneyUtil.moneyMul(fruit.getPrice(),kilos);
        for(IStrategy strategy : itemStrategys) {
            fruitMoney = strategy.realPrice(createDate,fruitMoney);
        }
        this.amount = fruitMoney;
        System.out.println(fruit.getName() + " : " + this.amount);
    }
    public void addStragegys(IStrategy strategy){
        this.itemStrategys.add(strategy);
    }
}
